package com.furongji.education.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradePrecreateModel;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.furongji.education.domain.po.Order;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

@Data
@Component
@ConfigurationProperties(prefix = "alipay")
public class AlipayConfiguration {

    // 应用ID,您的APPID，收款账号既是您的APPID对应支付宝账号
    public String appId;

    // 应用私钥，就是工具生成的应用私钥
    public String merchantPrivateKey;

    // 支付宝公钥,对应APPID下的支付宝公钥。
    public String alipayPublicKey;

    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    public String notifyUrl;

    public String returnUrl;

    private String signType;

    private String charset;

    private String timeout = "50m";

    public String gatewayUrl;

    // 跳转支付宝支付
    public String pay(Order order) throws AlipayApiException {
        AlipayClient alipayClient = new DefaultAlipayClient(
                gatewayUrl,
                appId,
                merchantPrivateKey,
                "json",
                charset,
                alipayPublicKey,
                signType
        );

        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(returnUrl);
        alipayRequest.setNotifyUrl(notifyUrl);

        String bizContent = String.format("{"
                        + "\"out_trade_no\":\"%s\","
                        + "\"total_amount\":\"%.2f\","
                        + "\"subject\":\"%s\","
                        + "\"timeout_express\":\"%s\","
                        + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\""
                        + "}",
                order.getId(),
                order.getTotalPrice(),
                order.getCourseTitle(), // 你可以写死一个测试字符串也行，比如 "课程购买"
                timeout
        );
        alipayRequest.setBizContent(bizContent);

        return alipayClient.pageExecute(alipayRequest).getBody();
    }

    // 扫码支付
    public String precreatePay(Order order) throws AlipayApiException {
        AlipayClient alipayClient = new DefaultAlipayClient(
                gatewayUrl,
                appId,
                merchantPrivateKey,
                "json",
                charset,
                alipayPublicKey,
                signType,
                "50000",
                "50000"
        );

        AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
        AlipayTradePrecreateModel model = new AlipayTradePrecreateModel();

        model.setOutTradeNo(order.getId());
        model.setTotalAmount(order.getTotalPrice().toString());
        model.setSubject("课程购买");
        model.setProductCode("FACE_TO_FACE_PAYMENT");
        request.setBizModel(model);

        request.setNotifyUrl(notifyUrl);

        AlipayTradePrecreateResponse response = alipayClient.execute(request);
        if (response.isSuccess()) {
            return response.getQrCode(); // 返回二维码链接
        } else {
            throw new RuntimeException("支付宝预下单失败: " + response.getSubMsg());
        }
    }
}
